package com.example.myapplication01;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.annotation.SuppressLint;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.ImageView;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

public class AnimationActivity extends AppCompatActivity {




    @SuppressLint("MissingInflatedId")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_animation);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main4), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        Button startBtn = (Button) findViewById(R.id.anim);
        startBtn.animate().translationX(500)
                .rotationX(0.5f)
                .alpha(0.5f).x(500).y(500)
                .setDuration(3000)
                .setListener(new AnimatorListenerAdapter() {
                    @Override
                    public void onAnimationStart(Animator animation) {
                        super.onAnimationCancel(animation);
                    }
                });

    }


    // ObjectAnimator
    private static void objectAnimator(Button startBtn) {
        float translationY = startBtn.getTranslationY();
        ValueAnimator animator = ObjectAnimator.ofFloat(startBtn, "translationY", translationY, -500f, translationY);
        animator.setDuration(3000);
        animator.setRepeatCount(0);
        animator.start();
    }


    // ValueAnimator数值估值器
    private static void valueAnimatorInt(Button startBtn) {
        ValueAnimator valueAnimator = ValueAnimator.ofInt(startBtn.getLayoutParams().width, 500);
        valueAnimator.setDuration(3000);
        valueAnimator.setRepeatCount(1);
        valueAnimator.addUpdateListener(animation -> {
            // 获得每次变化后的属性值
            int currentValue = (Integer) animation.getAnimatedValue();

            // 每次值变化时，将值手动赋值给对象的属性
            // 即将每次变化后的值赋给按钮的宽度，这样就实现了按钮宽度属性的动态变化
            startBtn.getLayoutParams().width = currentValue;

            // 步骤4：刷新视图，即重新绘制，从而实现动画效果
            startBtn.requestLayout();
        });

        valueAnimator.start();
    }


    // 逐帧动画
    private void frameAnimation(Button startBtn) {
        Button stopBtn = (Button) findViewById(R.id.anim2);
        ImageView iv = (ImageView) findViewById(R.id.imageView);

        // 开启动画
        startBtn.setOnClickListener(v -> {
            // 1. 设置动画
            iv.setImageResource(R.drawable.seven);
            // 2. 获取动画对象
            AnimationDrawable animationDrawable = (AnimationDrawable) iv.getDrawable();
            // 3. 启动动画
            animationDrawable.start();
        });

        // 停止动画
        stopBtn.setOnClickListener(v -> {
            // 1. 设置动画
            iv.setImageResource(R.drawable.seven);
            // 2. 获取动画对象
            AnimationDrawable animationDrawable = (AnimationDrawable) iv.getDrawable();
            // 3. 停止动画
            animationDrawable.stop();
        });
    }


    // 组合动画
    private static void animationSet(Button mButton) {
        // 组合动画
        // Animation animation = AnimationUtils.loadAnimation(this, R.anim.view_animation);
        // mButton.startAnimation(animation);

        // 组合动画设置
        AnimationSet setAnimation = new AnimationSet(true);
        // 步骤1:创建组合动画对象(设置为true)

        // 步骤2:设置组合动画的属性
        // 特别说明以下情况
        // 因为在下面的旋转动画设置了无限循环(RepeatCount = INFINITE)
        // 所以动画不会结束，而是无限循环
        // 所以组合动画的下面两行设置是无效的
        setAnimation.setRepeatMode(Animation.RESTART);
        setAnimation.setRepeatCount(1);// 设置了循环一次,但无效

        // 步骤3:逐个创建子动画(方式同单个动画创建方式)
        // 子动画1:旋转动画
        Animation rotate = new RotateAnimation(0,360,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
        rotate.setDuration(1000);
        rotate.setRepeatMode(Animation.RESTART);
        rotate.setRepeatCount(Animation.INFINITE);

        // 子动画2:平移动画
        Animation translate = new TranslateAnimation(TranslateAnimation.RELATIVE_TO_PARENT,-0.5f,
                TranslateAnimation.RELATIVE_TO_PARENT,0.5f,
                TranslateAnimation.RELATIVE_TO_SELF,0
                ,TranslateAnimation.RELATIVE_TO_SELF,0);
        translate.setDuration(10000);

        // 子动画3:透明度动画
        Animation alpha = new AlphaAnimation(1,0);
        alpha.setDuration(3000);
        alpha.setStartOffset(7000);

        // 子动画4:缩放动画
        Animation scale1 = new ScaleAnimation(1,0.5f,1,0.5f,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
        scale1.setDuration(1000);
        scale1.setStartOffset(4000);

        // 步骤4:将创建的子动画添加到组合动画里
        setAnimation.addAnimation(alpha);
        setAnimation.addAnimation(rotate);
        setAnimation.addAnimation(translate);
        setAnimation.addAnimation(scale1);

        // 步骤5:播放动画
        mButton.startAnimation(setAnimation);
    }

    @Override
    public void finish() {
        super.finish();
        overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
        // Button mButton = (Button) findViewById(R.id.anim);
    }


    // 透明动画
    private static void alphaAnimation(Button mButton) {
        AlphaAnimation alphaAnimation = new AlphaAnimation(0, 1);
        alphaAnimation.setDuration(3000);
        alphaAnimation.setRepeatCount(Animation.INFINITE);

        mButton.startAnimation(alphaAnimation);
    }


    // 翻转动画
    private static void rotateAnimation(Button mButton) {
        RotateAnimation rotateAnimation = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        rotateAnimation.setDuration(3000);
        // rotateAnimation.setRepeatCount(Animation.INFINITE);
        mButton.startAnimation(rotateAnimation);
    }


    // 缩放动画
    private static void scaleAnimation(Button mButton) {
        /*
         *参数说明:
         * 1. fromX ：动画在水平方向X的结束缩放倍数
         * 2. toX ：动画在水平方向X的结束缩放倍数
         * 3. fromY ：动画开始前在竖直方向Y的起始缩放倍数
         * 4. toY：动画在竖直方向Y的结束缩放倍数
         * 5. pivotXType:缩放轴点的x坐标的模式
         * 6. pivotXValue:缩放轴点x坐标的相对值
         * 7. pivotYType:缩放轴点的y坐标的模式
         * 8. pivotYValue:缩放轴点y坐标的相对值
         * pivotXType = Animation.ABSOLUTE:缩放轴点的x坐标 =  View左上角的原点 在x方向 加上 pivotXValue数值的点(y方向同理)
         * pivotXType = Animation.RELATIVE_TO_SELF:缩放轴点的x坐标 = View左上角的原点 在x方向 加上 自身宽度乘上pivotXValue数值的值(y方向同理)
         * pivotXType = Animation.RELATIVE_TO_PARENT:缩放轴点的x坐标 = View左上角的原点 在x方向 加上 父控件宽度乘上pivotXValue数值的值 (y方向同理)
         */
        ScaleAnimation scaleAnimation = new ScaleAnimation(1f, 2f, 1f, 2f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        scaleAnimation.setDuration(3000);
        // scaleAnimation.setRepeatCount(Animation.INFINITE); // 重放次数，infinite时无限重复
        mButton.startAnimation(scaleAnimation);
    }


    // 平移动画
    private static void translateAnimation(Button mButton) {
        // 1. 创建动画对象并传入设置的动画效果xml文件
        // Animation translateAnimation = AnimationUtils.loadAnimation(this, R.anim.view_animation);

        /*
         * 参数说明
         * fromXDelta ：视图在水平方向x 移动的起始值
         * toXDelta ：视图在水平方向x 移动的结束值
         * fromYDelta ：视图在竖直方向y 移动的起始值
         * toYDelta：视图在竖直方向y 移动的结束值
         */
        TranslateAnimation translateAnimation = new TranslateAnimation(0, 500, 0, 500);
        translateAnimation.setDuration(3000);
        // 2. 播放动画
        mButton.startAnimation(translateAnimation);
    }
}